﻿@{
    Layout = "~/Views/Shared/PlanPartial/_LayoutEmpty.cshtml";
}
@section ScriptAndCss{
    <link href="@Url.Content("~/Content/css/plan.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/plan.js")" type="text/javascript"></script>
}
@using Wicresoft.UnifyShow.Business;
@{
    var group = ViewData["Group"] as PlanGroupModel;
}
<div class="padding5">
    <input type="hidden" id="groupType" value="@(group != null ? group.GroupType.ToString() : "")"/>
    <div style="padding-top: 10px">
        编号：<span>@(group != null ? group.ID.ToString() : "")</span>
    </div>
    <div style="padding-bottom: 10px">
        名称：<input type="text" id="groupName" value="@(group != null ? group.GroupName : "")" @((group != null && group.GroupType == 1) ? "disabled='disabled'" : "") />
    </div>
    <div class="easyui-panel" title="用户列表" data-options="tools:'#searchUser'">
        <table id="MyGroupUserList" style="height: 310px;" class="" pagination="true" url="/PlanManagement/GetGroupUser"
            fitcolumns="true" singleselect="true">
            <thead>
                <tr>
                    <th field="UserID" hidden="true"></th>
                    <th field="Company" width="180">所属单位</th>
                    <th field="Department" width="140">所属部门</th>
                    <th field="UserName" width="100">姓名</th>
                    <th field="Position" width="100">职务</th>
                    <th field="Ope" width="100" formatter="OpeRender">操作</th>
                </tr>
            </thead>
        </table>
    </div>
    @if ((group != null && group.GroupType == 2))
    {
        <div id="searchUser">
            <a style="width: 100px;" onclick="openSearchUser(false,true)">查找添加用户<span class="icon-search-small" style="margin-top: -2px;"></span></a>
        </div>
    }
</div>
@if ((group != null && group.GroupType == 2))
{
    <div class="padding5">
        <input type="button" value="保存" onclick="updateGroupName()" />
        <input type="button" value="关闭" onclick="closeWindow();" />
    </div>
}

<script type="text/javascript">
    var OpeRender = function (value, row, index) {
        var groupType = $('#groupType').val();
        if (groupType == "2") {
            row["Ope"] = true;
            return '<a class="deleteRow">删除</a>';
        }
        return "";
    };

    var updateGroupName = function () {
        $.post('/PlanManagement/UpdateGroupName', { groupName: $('#groupName').val() }, function (res) {
            if (res == "OK") {
                $.messager.alert('提示', '保存成功!', 'info', function () {
                    $('#MyGroupUserList').datagrid('reload');
                });
            }
            if (res == "Error") {
                $.messager.alert('错误', '保存失败!', 'error');
            }
        });
    };

    var saveGroupUsers = function (data) {
        var users = [];
        $.each(data, function (i, user) {
            users.push(user["ID"].toString());
        });
        $.ajax({
            url: '/PlanManagement/AddGroupUsers',
            data: JSON.stringify({ users: users }),
            contentType: 'application/json',
            type: 'post',
            success: function (res) {
                if (res == "OK") {
                    $.messager.alert('提示', '保存成功!', 'info', function () {
                        $('#MyGroupUserList').datagrid('reload');
                    });
                }
                if (res == "Error") {
                    $.messager.alert('错误', '保存失败!', 'error');
                }
            }
        });
    };

    var addResponsible = function (data) {
        saveGroupUsers(data);
    };

    $(function () {
        $('#MyGroupUserList').datagrid({
            pageSize: 10,
            pageList: [10],
            pageNumber: 1,
            onClickCell: function (rowIndex, field, value) {
                if (field == "Ope" && value) {
                    $.messager.confirm('提醒', '确认删除这个成员吗？', function (r) {
                        if (r) {
                            $.post('/PlanManagement/DeleteGroupUser', {
                                userID: $('#MyGroupUserList').datagrid('getData').rows[rowIndex]["UserID"]
                            }, function (res) {
                                if (res == "OK") {
                                    $.messager.alert('提示', '删除成功!', 'info', function () {
                                        $('#MyGroupUserList').datagrid('reload');
                                    });
                                }
                                if (res == "Error") {
                                    $.messager.alert('错误', '删除失败!', 'error');
                                }
                            });
                        }
                    });
                }
            }
        });

        var p1 = $('#MyGroupUserList').datagrid('getPager');
        $(p1).pagination({
            beforePageText: '第',
            afterPageText: '页&nbsp;共&nbsp;{pages}&nbsp;页',
            displayMsg: '当前显示{from} - {to}&nbsp;条记录&nbsp;共&nbsp;{total}&nbsp;条记录'

        });
    });

</script>

